package com.clz.appdesign.myapp.kotlin

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Toast
import com.blankj.utilcode.util.LogUtils
import com.clz.appdesign.myapp.R
import kotlinx.android.synthetic.main.activity_kotlin.*

class KotlinActivity : AppCompatActivity(), View.OnClickListener {

    override fun onClick(v: View?) {
        var id = v!!.id
        when (id) {
            R.id.tv -> Toast.makeText(this, tv.text.toString(), Toast.LENGTH_SHORT).show()
        }
    }

    private var tag = "KotlinActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin)
        LogUtils.i(sum(3, 6).toString())
//        Log.d(tag,sum(3,5).toString())
        //vars("1","2","3","4","5")
        vars(1, 2, 3, 4, 5)
        tv.text = "哈哈"
        tv.setOnClickListener(this)
        val sumLambda: (Int, Int) -> Int = { x, y -> x + y }
        var message = sumLambda(3, 4)
        println(message)
        stringMoudle()
        //数组
        val a = arrayOf(1, 2, 3)
        val b = Array(3, { i -> i * 2 })
        println(a[0])
        println(b[1])
        for (item in a) {
            print(item)
        }
        //
        val d = if (3 < 5) 3 else 4
        when (d) {
            3 -> 4
            4 -> 5
            in 1..10 -> print("哈哈")
            else -> {
                5
            }
        }
        var person = Person("小明")
        var name = person.name
        person.age = 13
        //只读集合
        var list = listOf<String>()
        //可修改集合
        var multableList = mutableListOf<String>()
        multableList.add("哈哈")
        var map = mapOf<String, Any>()
        var multableMap = mutableMapOf<String, Any>()
        tv.setOnClickListener(this)
        var i :Int = 0
        val c = (i until 10).toList()
        var size = c.size
       LogUtils.iTag("HHA",size.toString())
    }

    /**
     * 字符串模板
     * $ 表示一个变量名或者变量值
     * $varName 表示变量值
     * ${varName.fun()} 表示变量的方法返回值:
     */
    private fun stringMoudle() {
        var a = 1
        var s1 = "a is $a"
        a = 2
        var s3 = "${s1.replace("is", "was")},but now is $a"
    }

    private fun sum(a: Int, b: Int): Int {
        return a + b
    }

    /**
     * 可变长参数函数
     */
    private fun vars(vararg v: Int) {
        for (vt in v) {
            print(vt)
        }
    }
}
